/*
    api:"获取用户信息"
    name:"陈小洋"
    time：2021.1.12
* */

const User = require('../../../control/User/User');
const tools = require('../../../tools/public');

/**
 * @api {post} /user/getUserInfo 3.获取用户信息
 * @apiGroup User
 *
 * @apiHeader {String} authentication token
 * @apiParamExample {json} Request-Example
 *{
 *   "authentication":"GmrahMAJrjBQwX6cRD5mPjpTN4w79Eep"
 *}
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "code": 200,
 *   "msg": "获取用户信息成功！",
 *   "data": {
            "_id": "600ada086f506451a49231f0",
            "Account": "15892023484",
            "Token": "7B5ttrdsknmEbFBXWKPEizhWrp5Ypc7D",
            "NickName": "",
            "Avatar": "",
            "Introduce": "",
            "sex": null,
            "birth": null,
            "Register": null,
            "Region": "",
            "IsAbleUse": true,
            "RemainingLogins": null,
            "Identity": "",
            "__v": 0
        }
 * }
 */
module.exports = async function (req, res, next) {
    // TODO 添加cookie  实现cookie+token双验证
    // TODO 记得在查询数据为空的时候清空cookie    res.clearCookie('aaa')

    let {UserId} = req.body,
        result = null;
    let {authentication} = req.headers;

    try {
        if (!authentication && !UserId) return res.send(tools.ReturnData(204, '参数错误,请认真阅读接口文档！'));
        result = await User.Sel_quoteType('LoginIp.Token', authentication, {LoginIp: 0})
        // result = await User.Sel_quoteType('_id', UserId, {LoginIp: 0})
        if (result.length >= 1) {
            res.send(tools.ReturnData(200, '获取用户信息成功！', result[0]));
        } else {
            res.send(tools.ReturnData(204, '该账号不存在！'));
        }
    } catch (e) {
        console.log(e)
        return res.send(tools.ReturnData(500, '系统内部错误！'));
    }
};